package de.gwtprime.server.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.appengine.api.channel.ChannelPresence;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;

public class ChannelConnectedServlet extends HttpServlet{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(ChannelConnectedServlet.class);

    private void process(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
        logger.debug("process channel creation / destruction");
        //do stuff here
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                   throws IOException, ServletException {        
       process(req, resp);
       
       ChannelService channelService = ChannelServiceFactory.getChannelService();
       ChannelPresence presence = channelService.parsePresence(req);
       
       logger.debug("channel client id: "+presence.clientId());
       logger.debug("channel is connected"+presence.isConnected());
       
     }
    
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
    	process(req, resp);
    }
}